home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / Choker.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  16.9 KB  |  597 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from random import randrange
  5.  
  6. class Choker:
  7.     
  8.     def __init__(self, max_uploads, schedule, done = (lambda : False), min_uploads = None):
  9.         self.max_uploads = max_uploads
  10.         if min_uploads is None:
  11.             min_uploads = max_uploads
  12.         
  13.         self.min_uploads = min_uploads
  14.         self.schedule = schedule
  15.         self.connections = []
  16.         self.count = 0
  17.         self.done = done
  18.         schedule(self._round_robin, 10)
  19.  
  20.     
  21.     def _round_robin(self):
  22.         self.schedule(self._round_robin, 10)
  23.         self.count += 1
  24.         if self.count % 3 == 0:
  25.             for i in xrange(len(self.connections)):
  26.                 u = self.connections[i].get_upload()
  27.                 if u.is_choked() and u.is_interested():
  28.                     self.connections = self.connections[i:] + self.connections[:i]
  29.                     break
  30.                     continue
  31.                 self
  32.             
  33.         
  34.         self._rechoke()
  35.  
  36.     
  37.     def _snubbed(self, c):
  38.         if self.done():
  39.             return False
  40.         
  41.         return c.get_download().is_snubbed()
  42.  
  43.     
  44.     def _rate(self, c):
  45.         if self.done():
  46.             return c.get_upload().get_rate()
  47.         else:
  48.             return c.get_download().get_rate()
  49.  
  50.     
  51.     def _rechoke(self):
  52.         preferred = []
  53.         for c in self.connections:
  54.             if not self._snubbed(c) and c.get_upload().is_interested():
  55.                 preferred.append((-self._rate(c), c))
  56.                 continue
  57.         
  58.         preferred.sort()
  59.         del preferred[self.max_uploads - 1:]
  60.         preferred = [ x[1] for x in preferred ]
  61.         count = len(preferred)
  62.         hit = False
  63.         for c in self.connections:
  64.             u = c.get_upload()
  65.             if c in preferred:
  66.                 u.unchoke()
  67.                 continue
  68.             []
  69.             if count < self.min_uploads or not hit:
  70.                 u.unchoke()
  71.                 if u.is_interested():
  72.                     count += 1
  73.                     hit = True
  74.                 
  75.             u.is_interested()
  76.             u.choke()
  77.         
  78.  
  79.     
  80.     def connection_made(self, connection, p = None):
  81.         if p is None:
  82.             p = randrange(-2, len(self.connections) + 1)
  83.         
  84.         self.connections.insert(max(p, 0), connection)
  85.         self._rechoke()
  86.  
  87.     
  88.     def connection_lost(self, connection):
  89.         self.connections.remove(connection)
  90.         if connection.get_upload().is_interested() and not connection.get_upload().is_choked():
  91.             self._rechoke()
  92.         
  93.  
  94.     
  95.     def interested(self, connection):
  96.         if not connection.get_upload().is_choked():
  97.             self._rechoke()
  98.         
  99.  
  100.     
  101.     def not_interested(self, connection):
  102.         if not connection.get_upload().is_choked():
  103.             self._rechoke()
  104.         
  105.  
  106.     
  107.     def change_max_uploads(self, newval):
  108.         
  109.         def foo(self = self, newval = newval):
  110.             self._change_max_uploads(newval)
  111.  
  112.         self.schedule(foo, 0)
  113.  
  114.     
  115.     def _change_max_uploads(self, newval):
  116.         self.max_uploads = newval
  117.         self._rechoke()
  118.  
  119.  
  120.  
  121. class DummyScheduler:
  122.     
  123.     def __init__(self):
  124.         self.s = []
  125.  
  126.     
  127.     def __call__(self, func, delay):
  128.         self.s.append((func, delay))
  129.  
  130.  
  131.  
  132. class DummyConnection:
  133.     
  134.     def __init__(self, v = 0):
  135.         self.u = DummyUploader()
  136.         self.d = DummyDownloader(self)
  137.         self.v = v
  138.  
  139.     
  140.     def get_upload(self):
  141.         return self.u
  142.  
  143.     
  144.     def get_download(self):
  145.         return self.d
  146.  
  147.  
  148.  
  149. class DummyDownloader:
  150.     
  151.     def __init__(self, c):
  152.         self.s = False
  153.         self.c = c
  154.  
  155.     
  156.     def is_snubbed(self):
  157.         return self.s
  158.  
  159.     
  160.     def get_rate(self):
  161.         return self.c.v
  162.  
  163.  
  164.  
  165. class DummyUploader:
  166.     
  167.     def __init__(self):
  168.         self.i = False
  169.         self.c = True
  170.  
  171.     
  172.     def choke(self):
  173.         if not (self.c):
  174.             self.c = True
  175.         
  176.  
  177.     
  178.     def unchoke(self):
  179.         if self.c:
  180.             self.c = False
  181.         
  182.  
  183.     
  184.     def is_choked(self):
  185.         return self.c
  186.  
  187.     
  188.     def is_interested(self):
  189.         return self.i
  190.  
  191.  
  192.  
  193. def test_round_robin_with_no_downloads():
  194.     s = DummyScheduler()
  195.     Choker(2, s)
  196.     if not len(s.s) == 1:
  197.         raise AssertionError
  198.     if not s.s[0][1] == 10:
  199.         raise AssertionError
  200.     s.s[0][0]()
  201.     del s.s[0]
  202.     if not len(s.s) == 1:
  203.         raise AssertionError
  204.     if not s.s[0][1] == 10:
  205.         raise AssertionError
  206.     s.s[0][0]()
  207.     del s.s[0]
  208.     s.s[0][0]()
  209.     del s.s[0]
  210.     s.s[0][0]()
  211.     del s.s[0]
  212.  
  213.  
  214. def test_resort():
  215.     s = DummyScheduler()
  216.     choker = Choker(1, s)
  217.     c1 = DummyConnection()
  218.     c2 = DummyConnection(1)
  219.     c3 = DummyConnection(2)
  220.     c4 = DummyConnection(3)
  221.     c2.u.i = True
  222.     c3.u.i = True
  223.     choker.connection_made(c1)
  224.     if not not (c1.u.c):
  225.         raise AssertionError
  226.     choker.connection_made(c2, 1)
  227.     if not not (c1.u.c):
  228.         raise AssertionError
  229.     if not not (c2.u.c):
  230.         raise AssertionError
  231.     choker.connection_made(c3, 1)
  232.     if not not (c1.u.c):
  233.         raise AssertionError
  234.     if not c2.u.c:
  235.         raise AssertionError
  236.     if not not (c3.u.c):
  237.         raise AssertionError
  238.     c2.v = 2
  239.     c3.v = 1
  240.     choker.connection_made(c4, 1)
  241.     if not not (c1.u.c):
  242.         raise AssertionError
  243.     if not c2.u.c:
  244.         raise AssertionError
  245.     if not not (c3.u.c):
  246.         raise AssertionError
  247.     if not not (c4.u.c):
  248.         raise AssertionError
  249.     choker.connection_lost(c4)
  250.     if not not (c1.u.c):
  251.         raise AssertionError
  252.     if not c2.u.c:
  253.         raise AssertionError
  254.     if not not (c3.u.c):
  255.         raise AssertionError
  256.     s.s[0][0]()
  257.     if not not (c1.u.c):
  258.         raise AssertionError
  259.     if not c2.u.c:
  260.         raise AssertionError
  261.     if not not (c3.u.c):
  262.         raise AssertionError
  263.  
  264.  
  265. def test_interest():
  266.     s = DummyScheduler()
  267.     choker = Choker(1, s)
  268.     c1 = DummyConnection()
  269.     c2 = DummyConnection(1)
  270.     c3 = DummyConnection(2)
  271.     c2.u.i = True
  272.     c3.u.i = True
  273.     choker.connection_made(c1)
  274.     if not not (c1.u.c):
  275.         raise AssertionError
  276.     choker.connection_made(c2, 1)
  277.     if not not (c1.u.c):
  278.         raise AssertionError
  279.     if not not (c2.u.c):
  280.         raise AssertionError
  281.     choker.connection_made(c3, 1)
  282.     if not not (c1.u.c):
  283.         raise AssertionError
  284.     if not c2.u.c:
  285.         raise AssertionError
  286.     if not not (c3.u.c):
  287.         raise AssertionError
  288.     c3.u.i = False
  289.     choker.not_interested(c3)
  290.     if not not (c1.u.c):
  291.         raise AssertionError
  292.     if not not (c2.u.c):
  293.         raise AssertionError
  294.     if not not (c3.u.c):
  295.         raise AssertionError
  296.     c3.u.i = True
  297.     choker.interested(c3)
  298.     if not not (c1.u.c):
  299.         raise AssertionError
  300.     if not c2.u.c:
  301.         raise AssertionError
  302.     if not not (c3.u.c):
  303.         raise AssertionError
  304.     choker.connection_lost(c3)
  305.     if not not (c1.u.c):
  306.         raise AssertionError
  307.     if not not (c2.u.c):
  308.         raise AssertionError
  309.  
  310.  
  311. def test_robin_interest():
  312.     s = DummyScheduler()
  313.     choker = Choker(1, s)
  314.     c1 = DummyConnection(0)
  315.     c2 = DummyConnection(1)
  316.     c1.u.i = True
  317.     choker.connection_made(c2)
  318.     if not not (c2.u.c):
  319.         raise AssertionError
  320.     choker.connection_made(c1, 0)
  321.     if not not (c1.u.c):
  322.         raise AssertionError
  323.     if not c2.u.c:
  324.         raise AssertionError
  325.     c1.u.i = False
  326.     choker.not_interested(c1)
  327.     if not not (c1.u.c):
  328.         raise AssertionError
  329.     if not not (c2.u.c):
  330.         raise AssertionError
  331.     c1.u.i = True
  332.     choker.interested(c1)
  333.     if not not (c1.u.c):
  334.         raise AssertionError
  335.     if not c2.u.c:
  336.         raise AssertionError
  337.     choker.connection_lost(c1)
  338.     if not not (c2.u.c):
  339.         raise AssertionError
  340.  
  341.  
  342. def test_skip_not_interested():
  343.     s = DummyScheduler()
  344.     choker = Choker(1, s)
  345.     c1 = DummyConnection(0)
  346.     c2 = DummyConnection(1)
  347.     c3 = DummyConnection(2)
  348.     c1.u.i = True
  349.     c3.u.i = True
  350.     choker.connection_made(c2)
  351.     if not not (c2.u.c):
  352.         raise AssertionError
  353.     choker.connection_made(c1, 0)
  354.     if not not (c1.u.c):
  355.         raise AssertionError
  356.     if not c2.u.c:
  357.         raise AssertionError
  358.     choker.connection_made(c3, 2)
  359.     if not not (c1.u.c):
  360.         raise AssertionError
  361.     if not c2.u.c:
  362.         raise AssertionError
  363.     if not c3.u.c:
  364.         raise AssertionError
  365.     f = s.s[0][0]
  366.     f()
  367.     if not not (c1.u.c):
  368.         raise AssertionError
  369.     if not c2.u.c:
  370.         raise AssertionError
  371.     if not c3.u.c:
  372.         raise AssertionError
  373.     f()
  374.     if not not (c1.u.c):
  375.         raise AssertionError
  376.     if not c2.u.c:
  377.         raise AssertionError
  378.     if not c3.u.c:
  379.         raise AssertionError
  380.     f()
  381.     if not c1.u.c:
  382.         raise AssertionError
  383.     if not c2.u.c:
  384.         raise AssertionError
  385.     if not not (c3.u.c):
  386.         raise AssertionError
  387.  
  388.  
  389. def test_connection_lost_no_interrupt():
  390.     s = DummyScheduler()
  391.     choker = Choker(1, s)
  392.     c1 = DummyConnection(0)
  393.     c2 = DummyConnection(1)
  394.     c3 = DummyConnection(2)
  395.     c1.u.i = True
  396.     c2.u.i = True
  397.     c3.u.i = True
  398.     choker.connection_made(c1)
  399.     choker.connection_made(c2, 1)
  400.     choker.connection_made(c3, 2)
  401.     f = s.s[0][0]
  402.     f()
  403.     if not not (c1.u.c):
  404.         raise AssertionError
  405.     if not c2.u.c:
  406.         raise AssertionError
  407.     if not c3.u.c:
  408.         raise AssertionError
  409.     f()
  410.     if not not (c1.u.c):
  411.         raise AssertionError
  412.     if not c2.u.c:
  413.         raise AssertionError
  414.     if not c3.u.c:
  415.         raise AssertionError
  416.     f()
  417.     if not c1.u.c:
  418.         raise AssertionError
  419.     if not not (c2.u.c):
  420.         raise AssertionError
  421.     if not c3.u.c:
  422.         raise AssertionError
  423.     f()
  424.     if not c1.u.c:
  425.         raise AssertionError
  426.     if not not (c2.u.c):
  427.         raise AssertionError
  428.     if not c3.u.c:
  429.         raise AssertionError
  430.     f()
  431.     if not c1.u.c:
  432.         raise AssertionError
  433.     if not not (c2.u.c):
  434.         raise AssertionError
  435.     if not c3.u.c:
  436.         raise AssertionError
  437.     choker.connection_lost(c3)
  438.     if not c1.u.c:
  439.         raise AssertionError
  440.     if not not (c2.u.c):
  441.         raise AssertionError
  442.     f()
  443.     if not not (c1.u.c):
  444.         raise AssertionError
  445.     if not c2.u.c:
  446.         raise AssertionError
  447.     choker.connection_lost(c2)
  448.     if not not (c1.u.c):
  449.         raise AssertionError
  450.  
  451.  
  452. def test_connection_made_no_interrupt():
  453.     s = DummyScheduler()
  454.     choker = Choker(1, s)
  455.     c1 = DummyConnection(0)
  456.     c2 = DummyConnection(1)
  457.     c3 = DummyConnection(2)
  458.     c1.u.i = True
  459.     c2.u.i = True
  460.     c3.u.i = True
  461.     choker.connection_made(c1)
  462.     choker.connection_made(c2, 1)
  463.     f = s.s[0][0]
  464.     if not not (c1.u.c):
  465.         raise AssertionError
  466.     if not c2.u.c:
  467.         raise AssertionError
  468.     f()
  469.     if not not (c1.u.c):
  470.         raise AssertionError
  471.     if not c2.u.c:
  472.         raise AssertionError
  473.     f()
  474.     if not not (c1.u.c):
  475.         raise AssertionError
  476.     if not c2.u.c:
  477.         raise AssertionError
  478.     choker.connection_made(c3, 1)
  479.     if not not (c1.u.c):
  480.         raise AssertionError
  481.     if not c2.u.c:
  482.         raise AssertionError
  483.     if not c3.u.c:
  484.         raise AssertionError
  485.     f()
  486.     if not c1.u.c:
  487.         raise AssertionError
  488.     if not c2.u.c:
  489.         raise AssertionError
  490.     if not not (c3.u.c):
  491.         raise AssertionError
  492.  
  493.  
  494. def test_round_robin():
  495.     s = DummyScheduler()
  496.     choker = Choker(1, s)
  497.     c1 = DummyConnection(0)
  498.     c2 = DummyConnection(1)
  499.     c1.u.i = True
  500.     c2.u.i = True
  501.     choker.connection_made(c1)
  502.     choker.connection_made(c2, 1)
  503.     f = s.s[0][0]
  504.     if not not (c1.u.c):
  505.         raise AssertionError
  506.     if not c2.u.c:
  507.         raise AssertionError
  508.     f()
  509.     if not not (c1.u.c):
  510.         raise AssertionError
  511.     if not c2.u.c:
  512.         raise AssertionError
  513.     f()
  514.     if not not (c1.u.c):
  515.         raise AssertionError
  516.     if not c2.u.c:
  517.         raise AssertionError
  518.     f()
  519.     if not c1.u.c:
  520.         raise AssertionError
  521.     if not not (c2.u.c):
  522.         raise AssertionError
  523.     f()
  524.     if not c1.u.c:
  525.         raise AssertionError
  526.     if not not (c2.u.c):
  527.         raise AssertionError
  528.     f()
  529.     if not c1.u.c:
  530.         raise AssertionError
  531.     if not not (c2.u.c):
  532.         raise AssertionError
  533.     f()
  534.     if not not (c1.u.c):
  535.         raise AssertionError
  536.     if not c2.u.c:
  537.         raise AssertionError
  538.  
  539.  
  540. def test_multi():
  541.     s = DummyScheduler()
  542.     choker = Choker(4, s)
  543.     c1 = DummyConnection(0)
  544.     c2 = DummyConnection(0)
  545.     c3 = DummyConnection(0)
  546.     c4 = DummyConnection(8)
  547.     c5 = DummyConnection(0)
  548.     c6 = DummyConnection(0)
  549.     c7 = DummyConnection(6)
  550.     c8 = DummyConnection(0)
  551.     c9 = DummyConnection(9)
  552.     c10 = DummyConnection(7)
  553.     c11 = DummyConnection(10)
  554.     choker.connection_made(c1, 0)
  555.     choker.connection_made(c2, 1)
  556.     choker.connection_made(c3, 2)
  557.     choker.connection_made(c4, 3)
  558.     choker.connection_made(c5, 4)
  559.     choker.connection_made(c6, 5)
  560.     choker.connection_made(c7, 6)
  561.     choker.connection_made(c8, 7)
  562.     choker.connection_made(c9, 8)
  563.     choker.connection_made(c10, 9)
  564.     choker.connection_made(c11, 10)
  565.     c2.u.i = True
  566.     c4.u.i = True
  567.     c6.u.i = True
  568.     c8.u.i = True
  569.     c10.u.i = True
  570.     c2.d.s = True
  571.     c6.d.s = True
  572.     c8.d.s = True
  573.     s.s[0][0]()
  574.     if not not (c1.u.c):
  575.         raise AssertionError
  576.     if not not (c2.u.c):
  577.         raise AssertionError
  578.     if not not (c3.u.c):
  579.         raise AssertionError
  580.     if not not (c4.u.c):
  581.         raise AssertionError
  582.     if not not (c5.u.c):
  583.         raise AssertionError
  584.     if not not (c6.u.c):
  585.         raise AssertionError
  586.     if not c7.u.c:
  587.         raise AssertionError
  588.     if not c8.u.c:
  589.         raise AssertionError
  590.     if not c9.u.c:
  591.         raise AssertionError
  592.     if not not (c10.u.c):
  593.         raise AssertionError
  594.     if not c11.u.c:
  595.         raise AssertionError
  596.  
  597.